<?php
/**
 * gestion des sessions, variables de session et cookies
 * @package PWE
 * @author Gmax <gmaxfr78@gmail.com>

 */
class session {
    private static $_sessid;
    
    /**
     * demmare ou récupère une session
     */
    public static function start(){
        session_start();
        self::$_sessid = session_id();
    }
    /**
     * renvoi l'identifiant de session
     * @return string
     */
    public static function getSessionId(){
        return self::$_sessid;
    }
    /**
     * recupère une variable de session $varName
     * @param string $varName
     * @return mixed|null
     */
    public static function get($varName){
        if(isset($_SESSION[$varName])){
            return $_SESSION[$varName];
        }else{
            return null;
        }
    }
    /**
     * enregistre une variable de session
     * @param string $varName
     * @param mixed $value
     * @return boolean
     */
    public static function set($varName,$value){
        if(is_array($value)) $value = serialize ($value);
        $_SESSION[$varName] = $value;
        return true;
    }
    /**
     * supprime une variable de session $varName
     * @param string $varName
     */
    public static function delete($varName){
        unset($_SESSION[$varName]);
    }
    /**
     * enregistre une variable dans un cookie
     * @param string $varName
     * @param mixed $value
     * @param int $time
     * @param string $path
     * @param string $domaine
     * @return boolean
     */
    public static function setCookie($varName,$value,$time,$path='/',$domaine=''){
        if(is_array($value)) $value = serialize ($value);
        if($domaine!==''){
            return setcookie($varName,$value,$time,$path,$domaine);
        }else{
            return setcookie($varName,$value,$time,$path);
        }
    }
    /**
     * détruit un cookie
     * @param string $varName
     * @param string $path
     * @param string $domaine
     * @return boolean
     */
    public static function deleteCookie($varName,$path='/',$domaine=''){
        if($domaine!==''){
            return setcookie($varName,"",time()-3600,$path,$domaine);
        }else{
            return setcookie($varName,"",time()-3600,$path);
        }
    }
}

?>
